package icu.qx.gbs.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

import icu.qx.gbs.domain.GbsBom;
import icu.qx.gbs.service.IGbsBomService;
import icu.qx.gbs.service.IGbsGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.gbs.mapper.GbsBomMaterialMapper;
import icu.qx.gbs.domain.GbsBomMaterial;
import icu.qx.gbs.service.IGbsBomMaterialService;

/**
 * 物料配件Service业务层处理
 *
 * @author yong
 * @date 2024-06-21
 */
@Service
public class GbsBomMaterialServiceImpl implements IGbsBomMaterialService {
    @Autowired
    private GbsBomMaterialMapper gbsBomMaterialMapper;

    /**
     * 查询物料配件
     *
     * @param bmBomCode 物料配件主键
     * @return 物料配件
     */
    @Override
    public GbsBomMaterial selectGbsBomMaterialByBmBomCode(String bmBomCode) {
        return gbsBomMaterialMapper.selectGbsBomMaterialByBmBomCode(bmBomCode);
    }

    /**
     * 查询物料配件列表
     *
     * @param gbsBomMaterial 物料配件
     * @return 物料配件
     */
    @Override
    public List<GbsBomMaterial> selectGbsBomMaterialList(GbsBomMaterial gbsBomMaterial) {
        CopyOnWriteArrayList<GbsBomMaterial> gbsBomMaterials = new CopyOnWriteArrayList<>(gbsBomMaterialMapper.selectGbsBomMaterialList(gbsBomMaterial));
        gbsBomMaterials.forEach(e1 -> {
            CopyOnWriteArrayList<GbsBomMaterial> materials = new CopyOnWriteArrayList<>();
            gbsBomMaterials.forEach(e2 -> {
                if (e2.getBmMBomCode().equals(e1.getBmBomCode()) && e2.getBmMBomVersion().equals(e1.getBmBomVersion())) {
                    materials.add(e2);
                    gbsBomMaterials.remove(e2);
                }
            });
            e1.setGbsBomMaterials(materials);
        });
        return gbsBomMaterials;
    }


    /**
     * 新增物料配件
     *
     * @param gbsBomMaterial 物料配件
     * @return 结果
     */
    @Override
    public int insertGbsBomMaterial(GbsBomMaterial gbsBomMaterial) {
        return gbsBomMaterialMapper.insertGbsBomMaterial(gbsBomMaterial);
    }

    /**
     * 修改物料配件
     *
     * @param gbsBomMaterial 物料配件
     * @return 结果
     */
    @Override
    public int updateGbsBomMaterial(GbsBomMaterial gbsBomMaterial) {
        return gbsBomMaterialMapper.updateGbsBomMaterial(gbsBomMaterial);
    }

    /**
     * 批量删除物料配件
     *
     * @param bmBomCodes 需要删除的物料配件主键
     * @return 结果
     */
    @Override
    public int deleteGbsBomMaterialByBmBomCodes(String[] bmBomCodes) {
        return gbsBomMaterialMapper.deleteGbsBomMaterialByBmBomCodes(bmBomCodes);
    }

    /**
     * 删除物料配件信息
     *
     * @return 结果
     */
    @Override
    public int deleteGbsBomMaterialByBmBomCode(GbsBomMaterial gbsBomMaterial) {
        return gbsBomMaterialMapper.deleteGbsBomMaterialByBmBomCode(gbsBomMaterial);
    }
}
